package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.dto.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class CHUANGYOUAttr implements AdapterAttr {

    public static String getkey(SalePartner salePartner) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", salePartner.getOther());
        Map<String, Object> paramsSin = new HashMap<>();
        paramsSin.put("key", salePartner.getSaleKey());
        paramsSin.put("name", "租游乐");
        String result01 = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Get-Key", paramsSin, headers);
        String sign = JSONObject.parseObject(result01).getString("sign");
        System.out.println(result01);
        return sign;
    }

    public static String getSign(String token, String key) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", token);
        Map<String, Object> paramsSin = new HashMap<>();
        paramsSin.put("key", key);
        String result01 = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Get-Sign", paramsSin, headers);
        String sign = JSONObject.parseObject(result01).getString("sign");
        System.out.println(result01);
        return sign;
    }

    static String getGame(SalePartner salePartner){
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", salePartner.getOther());
        Map<String, Object> paramsSin = new HashMap<>();
        paramsSin.put("key", salePartner.getSaleKey());
        paramsSin.put("game_id", 48);
        String result01 = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Get-Sign", paramsSin, headers);
        String sign = JSONObject.parseObject(result01).getString("sign");
        Map<String, Object> params = new HashMap<>();
//        params.put("game_id", 25);
        params.put("sign", sign);
        String result = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Game-account-data", params, headers);
        return result;
    }


    public static void main(String[] args) {
//        CHUANGYOUAttr chuangyouAttr=new CHUANGYOUAttr();
        SalePartner salePartner = new SalePartner();
        salePartner.setSaleKey("LGJpvKiMfzo58Wh0lSy6rEkbH4wtO7Tc");
        salePartner.setOther("e4cc7136-011346d481becb63-b3a5b2b5-d8ba0d85-3d13a40ae460\n");
//        salePartner.setTwoOther("e4677eb8-832f47c3994cb38c-dc9b152e-36a782db-5a429d04c3d3");
//        salePartner.setThreeOther("LGJpvKiMfzo58Wh0lSy6rEkbH4wtO7Tc");
//      String  token = chuangyouAttr.getToken();
//     //   List<GamesRes> token = chuangyouAttr.getGames(salePartner);
//        System.out.println(token);
        String game = getGame(salePartner);
        System.out.println(game);
    }
    public String getToken() {
        Map<String, Object> params = new HashMap<>();
        params.put("account", "18093522409");
        params.put("password", "aa120120");
        String result = HttpUtils.doPostFormData("https://w1.cy227.com/docking/User-GetToken", params, null);
        String data = JSONObject.parseObject(result).getString("data");
        String token = JSONObject.parseObject(data).getString("token");
        //redisCache.setCacheObject("CY_TOEKN",token);
        return token;
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", salePartner.getOther());
        Map<String, Object> params = new HashMap<>();
        params.put("firstLetter", "");
        params.put("name", "");
        params.put("object", "false");
        params.put("type", "");
        params.put("sign", getSign(salePartner.getOther(), salePartner.getSaleKey()));
        String result = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Game-list", params, headers);
        String data = JSONObject.parseObject(result).getString("data");
        List<JSONObject> jsonObjects = JSONObject.parseArray(data, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", salePartner.getOther());
        Map<String, Object> paramsSin = new HashMap<>();
        paramsSin.put("key", salePartner.getSaleKey());
        paramsSin.put("game_id", gameId);
        String result01 = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Get-Sign", paramsSin, headers);
        String sign = JSONObject.parseObject(result01).getString("sign");
        Map<String, Object> params = new HashMap<>();
        params.put("sign", sign);
        String result = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Game-server", params, headers);
        System.out.println(result);
        String data1 = JSONObject.parseObject(result).getString("data");
        List<JSONObject> data = JSONObject.parseArray(data1, JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();

        for (JSONObject datum : data) {
            String id = datum.getString("id");
            String name = datum.getString("name");
            InterServerRes interServer = new InterServerRes();
            interServer.setId(id);
            interServer.setName(name);
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("token", salePartner.getOther());
        Map<String, Object> paramsSin = new HashMap<>();
        paramsSin.put("key", salePartner.getSaleKey());
        paramsSin.put("game_id", gameId);
        String result01 = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Get-Sign", paramsSin, headers);
        String sign = JSONObject.parseObject(result01).getString("sign");
        Map<String, Object> params = new HashMap<>();
//        params.put("game_id", 25);
        params.put("sign", sign);
        String result = HttpUtils.doPostFormData("https://w1.cy227.com/docking/Game-game-data", params, headers);
        String data1 = JSONObject.parseObject(result).getString("data");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        List<JSONObject> data = JSONObject.parseArray(data1, JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("SIGNLE")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("CHECK")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("NUMBER")).collect(Collectors.toList());
        }
        if(!CollectionUtils.isEmpty(collect)) {
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                interAttrRes.setUuid(UuidUtils.generateUuid());
                String id = object.getString("id");
                String name = object.getString("name");
                String customFieldsValueList = object.getString("options");
                interAttrRes.setId(id);
                interAttrRes.setName(name);
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                List<JSONObject> list = JSONObject.parseArray(customFieldsValueList, JSONObject.class);
                if (!CollectionUtils.isEmpty(list)) {
                    for (JSONObject s : list) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setUuid(UuidUtils.generateUuid());
                        interAttrRes1.setId(s.getString("id"));
                        interAttrRes1.setName(s.getString("fieldName"));
                        interAttrResList1.add(interAttrRes1);
                    }
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }else {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UuidUtils.generateUuid());
            interAttrRes.setId("该游戏没有该匹配项 保存即可！");
            interAttrRes.setName("该游戏没有该匹配项 保存即可！");
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}